#pragma once

// 红黑树的查找 + 验证

#include <iostream>
#include <assert.h>
using namespace std;

//红黑树是一颗自平衡二叉搜索树
//红黑树是树, 树是由一个一个节点组成 ,故这里封装节点
//红黑树是 K/V 结构


//枚举
enum Colour
{
	RED,
	BLACK
};

//封装树的节点类 -- 一个节点里包含节点的指针, 和对应值 , 因为是树需要 左孩子 , 右孩子 , 值 , 父亲节点(方便更新)
template <class K, class V>
struct RBTreeNode
{
	pair<K,V> _kv; // 存的值
	RBTreeNode<K,V>* _left;
	RBTreeNode<K,V>* _right;
	RBTreeNode<K,V>* _parent; // 方便后续往上更新
	Colour _col; // 颜色
	//构造
	RBTreeNode(const pair<K, V>& kv)
		:_kv(kv)
		,_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_col(RED)
	{}
};

//红黑树类
template <class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:

	bool Insert(const pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;

			return true;
		}

		//前提搜索树规则插入
		Node* cur = _root;
		Node* parent = nullptr;

		while (cur)
		{
			//键值对比较的是 Key
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				assert(false);
			}
		}

		//插入
		cur = new Node(kv);
		cur->_col = RED; // 插入颜色必须是红色
		if (cur->_kv.first > parent->_kv.first)
			parent->_right = cur;
		else
			parent->_left = cur;

		//链接父亲
		cur->_parent = parent;

		// 看叔叔 ... 更新 
		// ..... 

		//最坏情况更新到根 , 也就是 parent 为空 , 就是最坏情况
		
		//为红就更新
		while (parent && parent->_col == RED) //这里注意 parent可能为空
		{
			Node* grandfather = parent->_parent;
			if (parent == grandfather->_left)
			{
				//   g 
				//  p  u
				// c
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					//直接变色
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					//继续向上
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					//旋转 + 变色
					
					//   g 
					//  p  u
					// c
					if (cur == parent->_left)
					{
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					//   g 
					//  p  u
					//   c
					else
					{
						RotateL(parent);
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}

			}
			else
			{
				Node* uncle = grandfather->_left;
				// 叔叔存在且为红，- 变色即可
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					// 继续往上处理
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{

					//   g 
					//  u  p
					//      c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					//   g 
					//  u  p
					//    c
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;

				}
			}

		}

		_root->_col = BLACK; // 必须保证根是黑的
		return true;
	}

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* pParent = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}

			subL->_parent = pParent;
		}
	}

	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* parentParent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;
		if (parentParent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == parentParent->_left)
			{
				parentParent->_left = subR;
			}
			else
			{
				parentParent->_right = subR;
			}
			subR->_parent = parentParent;
		}
	}

	//查找是按照 Key 值
	Node* Find(const K& key)
	{
		Node* cur = _root;
		//按照搜索树查找
		while (cur)
		{
			if (cur->_kv.first < key)
				cur = cur->_right;
			else if(cur->_kv.first > key)
				cur = cur->_left;
			else
				//相等返回
				return cur;
		}

		//为空就找不到
		return nullptr;
	}

	//红黑树的检查 -- 检查是否为红黑树
	bool Check(Node* root , int BlackNum, const int RefNum) // RefNum -- 一条路径上黑色节点数量的值
	{
		//前序遍历

		//检查 4
		if (root == nullptr)
		{
			//走到空, 说明一条路径走完了
			if (RefNum != BlackNum)
			{
				cout << "该路径不符合黑色节点数量相等" << endl;
				return false;
			}

			//相等 , 返回
			return true;
		}

		//检查 3
		if (root->_col == RED && root->_parent->_col == RED)
		{
			cout << "存在红红连续节点" << endl;
			return false;
		}

		if (root->_col == BLACK)
		{
			BlackNum++;  // 黑色节点数量 ++ 
		}

		return Check(root->_left, BlackNum, RefNum) && Check(root->_right, BlackNum, RefNum);
	}

	//红黑树判平衡
	bool IsBalance()
	{

		if (_root == nullptr)
			return true;

		if (_root->_col == RED)
			return false;

		//从根节点开始找一条路径算出黑色节点参考值
		Node* cur = _root;
		int RefNum = 0;
		while (cur)
		{
			if (cur->_col == BLACK)
				++RefNum;
			
			cur = cur->_left;
		}

		return Check(_root,0, RefNum);
	}

	void InOrder()
	{
		_InOrder(_root);
	}

private:

	void _InOrder(Node* root)
	{
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->_kv.first << "->" << root->_kv.second << endl;
		_InOrder(root->_right);
	}
private:
	//红黑树存的是一个根节点 , 通过根节点可以找到其它节点
	Node* _root = nullptr;
};






